;(function(){
  'use strict';
  angular.module('controllers.workOrderData.auditData')
  .controller('notPassNCController',notPassNCController);
  function notPassNCController($scope,auditDataService,$timeout,notPassNCService,COREDATA,CoreLoading,workOrderDataService){
  	$scope.flag = {
  		state: true,
  		show: 'view',
  		arrow: false
  	};
  	$scope.initQuery = function(){
		auditDataService.queryWorkDataType(function(res){
			$scope.workDataTypes = res.data;
			$scope.search();
		});
	};
	$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/NCDistributed/export';
  	//页面查询项绑定对象
  	var date = new Date();
  	var year = date.getFullYear();
  	var month = date.getMonth();
  	$scope.params = {
  		startTime: new Date(year,month-1,1),
  		endTime: new Date(year,month,0),
  		changeTypeCode: '1',
  		workOrderTypeCode : '15601',
  		timeType: '1'
  	};
  	var myEcharts = echarts.init(document.getElementById('doc'));
  	//查询
  	$scope.search = function(){
		var changeTypeName = $scope.params.changeTypeCode === '1' ? '各科室' : '各系统';
		$scope.changeType = $scope.params.changeTypeCode === '1' ? '科室' : '业务系统';
		$scope.flag.state = !$scope.flag.state;
		var xAxisData = [];
		var seriesData = [];
		var option = {};
		if(!$scope.flag.state){
			$scope.send = notPassNCService.fmtSendData($scope.params);
			var timeTitle = auditDataService.fmtDateTitle($scope.send);
			//标题:时间+期间+各系统或科室+工单类型+NC分布统计;
			$scope.name = auditDataService.getName($scope.params.workOrderTypeCode,$scope.workDataTypes);
			$scope.send.workOrderTypeCode = $scope.params.workOrderTypeCode;
			$scope.send.title = timeTitle + '期间' + changeTypeName + $scope.name + 'NC分布统计';
			CoreLoading.setState(true);
			notPassNCService.query($scope.send,function(res){
				if(res.state === '10000' && res.data.length >= 1){
					myEcharts.clear();
					angular.forEach(res.data,function(item){
						xAxisData.push(item.businessSys);
						seriesData.push(item.ncNum);
					});
					$scope.items = res.data;
					$scope.title = $scope.send.title;
					option = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.title,$scope.name);
					option.tooltip.formatter = workOrderDataService.getFormatterFn('个');
					option.yAxis[0] = workOrderDataService.formatterYAxis('单位:(个)','');
					myEcharts.setOption(option);
					$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
					$scope.flag.arrow = true;
					$scope.flag.show = 'view';
				}else if(res.data.length === 0){
					workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
				}else {
					workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
				}
				CoreLoading.setState(false);
			});
		}
	};

	//监听表单,当表单有错时,禁止查询
	$scope.$watch('myForm.$invalid', function(newValue) {
		newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
	});
  }
  notPassNCController.$inject = ['$scope','auditDataService','$timeout','notPassNCService','COREDATA','CoreLoading','workOrderDataService'];
})();